package secondStage.part5.网络编程.TCP通信程序;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName: [ServerTcpReceiveTest]
 * @Description: TCP通信程序，接收信息
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-06]
 * @Version: [v1.0]
 */
//服务器:接收数据，给出反馈
public class ServerTcpReceiveTest {
    public static void main(String[] args) throws IOException {
        //创建, 服务器端的Socket对象(ServerSocket)
        ServerSocket ss = new ServerSocket (10086);
        //监听客户端连接，返回一个Socket对象
        Socket s = ss.accept();
        //获取输入流，读数据，并把数据显示在控制台
        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys, 0, len);
        System.out.println("服务器："+data);
        //给出反馈
        OutputStream os = s.getOutputStream();
        os.write("数据已收到".getBytes());
        //释放资源.
        ss.close();
    }
}
